package com.hillky.emos.wx;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hillky.emos.wx.config.SystemConstants;
import com.hillky.emos.wx.domain.SysConfig;
import com.hillky.emos.wx.mapper.SysConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.List;

@SpringBootApplication
@Slf4j
public class EmosWxApiApplication {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    private SystemConstants constants;

    public static void main(String[] args) {
        SpringApplication.run(EmosWxApiApplication.class, args);
    }


    @PostConstruct  //加入这个注解可以启动的时候执行
    public void init(){
        List<SysConfig> list = sysConfigMapper.selectList(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getStatus, 1));
        list.forEach(one->{
            String key = one.getParamKey();
            key= StrUtil.toCamelCase(key);
            String value = one.getParamValue();
            try {
                Field field = constants.getClass().getDeclaredField(key);
                field.set(constants,value);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                log.error("执行异常",e);
                e.printStackTrace();
            }
        });
    }

}
